var CreateTicket = {

    init: function () {
        var self = this;

        // Dual Box Select
        var db = jQuery('#dualselect').find('.ds_arrow button');	//get arrows of dual select
        var sel1 = jQuery('#dualselect select:first-child');		//get first select element
        var sel2 = jQuery('#dualselect select:last-child');			//get second select element

        sel1.empty(); //empty it first from dom.

        db.click(function () {
            var t = (jQuery(this).hasClass('ds_prev')) ? 0 : 1;	// 0 if arrow prev otherwise arrow next
            if (t) {
                sel1.find('option').each(function () {
                    if (jQuery(this).is(':selected')) {
                        jQuery(this).attr('selected', false);
                        var op = sel2.find('option:first-child');
                        sel2.append(jQuery(this));
                    }
                });
            } else {
                sel2.find('option').each(function () {
                    if (jQuery(this).is(':selected')) {
                        jQuery(this).attr('selected', false);
                        sel1.append(jQuery(this));
                    }
                });
            }
            return false;
        });

        $("#due_dt").mask("9999-99-99");

        $("#form").submit(function () {
            sel1.find('option').each(function () {
                jQuery(this).attr('selected', true);
            });
            return true;
        });

        $("#project_id").change(function () {
            var id = $(this).val();
            if (id) {
                for (var x in gblProjectMoreData) {
                    var pro_id = gblProjectMoreData[x]['Project']['id'];
                    if (pro_id == id) {
                        var branch_id = gblProjectMoreData[x]['Branch']['id'];
                        var department_id = gblProjectMoreData[x]['Department']['id'];

                        $("#form #branch_id").val(branch_id);
                        $("#form #department_id").val(department_id);
                        break;
                    }
                }
            }
        });
        $("#project_id").change();

    }
};

/* when the page is loaded */
$(document).ready(function () {
    CreateTicket.init();
});